package com.example.restaurant.controller;

import cn.dev33.satoken.stp.SaLoginConfig;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.restaurant.common.Log;
import com.example.restaurant.common.RestaurantException;
import com.example.restaurant.common.Result;
import com.example.restaurant.controller.form.AddPersonForm;
import com.example.restaurant.controller.form.AddUserForm;
import com.example.restaurant.controller.form.PutStorePersonNameForm;
import com.example.restaurant.model.User;
import com.example.restaurant.service.UserService;
import com.example.restaurant.service.dto.GetStoreNameDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author x
 * @since 2022-09-29
 */
@RestController
@Api("用户信息相关接口")
@Slf4j
@CrossOrigin("*")
public class UserController {

    @Resource
    private UserService userService;

    //账号重复验证
    @GetMapping("/checkUsername")
    public Result checkUsername(String username){
        boolean b = userService.checkUsername(username);
        return Result.success();
    }

    //用户注册
    @PostMapping("/addUser")
    @Log("录入用户信息")
    @ApiOperation("新增用户信息")
    @ApiImplicitParam("用户信息录入表单")
    public Result addUser(@Valid @RequestBody AddUserForm addUserForm){
        log.info("注册信息{}",addUserForm);
        boolean b = userService.addUser(addUserForm);
        return Result.success();
    }

    //用户登录
    @GetMapping("/doLogin")
    public Result login(String username,String password){
        User user = userService.doLogin(username, password);
        StpUtil.login(user.getId(),SaLoginConfig.setExtra("username",user.getUsername()).setExtra("roleId",user.getRoleId()).setExtra("userId",user.getId()));
//        System.out.println(StpUtil.getExtra("userId"));
        return Result.success(StpUtil.getTokenValue());
    }

    //===========================商户操作==============================

    //查询员工
    @GetMapping("/getPerson")
    public Result getPerson(String username){
        Object roleId = StpUtil.getExtra("roleId");
        String rId = String.valueOf(roleId);
        int i1 = Integer.parseInt(rId);
        if(i1!=1){
            throw new RestaurantException("您没有该操作权限");
        }
        Object userId = StpUtil.getExtra("userId");
        String userId1 = String.valueOf(userId);
        int i = Integer.parseInt(userId1);
        List<User> persons = userService.getPerson(i, username);
        return Result.success(persons);
    }

    //根据门店id和店长名查询
    @GetMapping("getStoreNames")
    @Log
    public Result getStoreNames(int storeId, String username,@RequestParam(defaultValue = "1") int current,@RequestParam(defaultValue = "5") int size){
        Object roleId = StpUtil.getExtra("roleId");
        String rId = String.valueOf(roleId);
        int i1 = Integer.parseInt(rId);
        if(i1!=1){
            throw new RestaurantException("您没有该操作权限");
        }
        log.info("{},{},{}",storeId,username,size);
        IPage<GetStoreNameDto> storeName = userService.getStoreName(storeId, username, current, size);
        return Result.success(storeName);
    }



    //新增员工
    @PostMapping("/addPerson")
    public Result addPerson(@RequestBody @Valid AddPersonForm addPersonForm){
        Object roleId = StpUtil.getExtra("roleId");
        String rId = String.valueOf(roleId);
        int i1 = Integer.parseInt(rId);
        if(i1!=1){
            throw new RestaurantException("您没有该操作权限");
        }
        boolean b = userService.addPerson(addPersonForm);
        return Result.success();
    }


    //修改员工信息
    @PutMapping("putStorePersonName")
    @Log
    public Result putStorePersonName(@RequestBody @Valid PutStorePersonNameForm putStorePersonNameForm){
        Object roleId = StpUtil.getExtra("roleId");
        String rId = String.valueOf(roleId);
        int i1 = Integer.parseInt(rId);
        if(i1!=1){
            throw new RestaurantException("您没有该操作权限");
        }
        boolean b = userService.putStorePersonName(putStorePersonNameForm);
        return Result.success();
    }

    //删除店长
    @DeleteMapping("delPersonById")
    @Log
    public Result delPersonById(int userId){
        Object roleId = StpUtil.getExtra("roleId");
        String rId = String.valueOf(roleId);
        int i1 = Integer.parseInt(rId);
        if(i1!=1){
            throw new RestaurantException("您没有该操作权限");
        }
        boolean b = userService.delPersonById(userId);
        return Result.success();
    }


}
